package logic

import (
	"context"
	"shop/shopRpc/shopRpc"
	"strconv"

	"shop/api/internal/svc"
	"shop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCartListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartListLogic {
	return &CartListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CartListLogic) CartList() (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userIdStr, ok := l.ctx.Value("user_id").(string)
	if !ok {
		return &types.Response{
			Code:    401,
			Message: "未授权",
			Data:    nil,
		}, nil
	}
	userID, _ := strconv.Atoi(userIdStr)

	list, err := l.svcCtx.ShopRpc.CartList(l.ctx, &shopRpc.CartListRequest{
		UserId: int64(userID),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: "购物车展示失败",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "购物车展示成功",
		Data:    list,
	}, nil
}
